package com.sqoop.client.client;

import com.sqoop.client.client.destinationgenerator.FixedReceiveDestinationGenerator;
import com.sqoop.client.client.destinationgenerator.FixedSenerDestinationGenerator;
import com.sqoop.commons.receive.MsgListener;
import com.sqoop.commons.receive.impl.MqMsgListener;
import com.sqoop.commons.send.impl.JmsClient;
import com.sqoop.core.destnation.MqMsgDestination;
import com.sqoop.core.futruecache.FutureCache;
import com.sqoop.core.futruecache.MsgFutureCache;
import com.sqoop.core.msg.Global;
import com.sqoop.core.msg.MsgDestination;
import com.sqoop.core.request.Request;
import com.sqoop.core.request.Response;
import net.sf.json.JSONObject;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by zw on 2017/11/13.
 */
public class ClientUtils {
    private static MsgListener msgListener = new MqMsgListener();
    private static FutureCache msgFutureCache = new MsgFutureCache();
    private static MsgReceiver msgReceiver = new MsgReceiver(msgListener,msgFutureCache);

    private static Set<MsgDestination> destinationSet = new HashSet<>();

    public static Response send(Request request){

        FixedReceiveDestinationGenerator fixedReceiveDestinationGenerator = new FixedReceiveDestinationGenerator(Global.getInstance().getUrl(),Global.getInstance().getQueuename(),Global.getInstance().getQueuetype());
        MqMsgDestination msgDestination = (MqMsgDestination) fixedReceiveDestinationGenerator.generator(request);
        //msgDestination.
        MsgDestination sender = new FixedSenerDestinationGenerator(Global.getInstance().getUrl(),Global.getInstance().getQueuename(),Global.getInstance().getQueuetype()).generator(request);
        request.setSender(sender);
        request.setReceiver(msgDestination);
        msgReceiver.register(sender);

        //通知服务端监听队列。。。  先有鸡 还是先有蛋。。
        // 目测 服务端必须要监听一个队列 管理数据。。
//        if (!destinationSet.contains(msgDestination)){
//
//
//        }

//        request.setReceiver(fixedReceiveDestinationGenerator.generator(request));
//        msgReceiver.register(sender);

        MsgClient client = new MsgClient(new JmsClient(),msgFutureCache);
        return client.send(request);



    }

    public static void main(String[] args) {
        SqoopRequest request = new SqoopRequest();
//        MqMsgDestination destination = new MqMsgDestination();
//        destination.setMqurl("failover:(tcp://192.168.130.192:61616,tcp://192.168.120.158:61616)");
//        destination.setQueuename("SqoopRequest");
//        destination.setQueuetype("queue");
//        request.setReceiver(destination);
        //request.se
        for (int i = 0;i<5;i++) {
            Response response = ClientUtils.send(request);
            //System.out.println(JSONObject.fromObject(response));
            if (response instanceof SqoopResponse) {
                SqoopResponse sqoopResponse = (SqoopResponse) response;
                System.out.println(sqoopResponse.getStr());
            }
        }
    }

}